<?php
/*************************************************************************
Class Pick_Core_Objects
----------------------------------------------------------------
PICKCO CMS Project
Company: PICKCO  Co., Ltd  
Email: admin@pick.vn
Last updated: 15/07/2008
**************************************************************************/
class Pick_Core_Objects extends Pick_Core_Model {
	
	var $objectInfo = "";
	// This field validate for field of table
	var $fieldValidate = array();
	// This field only for category	
	var $typeId = " AND 1=1";
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/
	function Pick_Core_Objects($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
	}
/*-----------------------------------------------------------------------*
* Function: getObjects
* Parameter: WHERE $key
* Return: Array of Info objects
*-----------------------------------------------------------------------*/   	
	function getObjects($page = 1, $condition = '1>0', $sort = array(), $items_per_page = 0) {
		if(!$page) $page = 1;
		$condition = $condition.$this->typeId;
		$start = ($page-1) * (int)$items_per_page;
		$results = $this->select('*', $condition, $sort, $start, $items_per_page);
		if($results) {
			$objectsInfo = array();
			foreach($results as $key => $result) {
				$objectsInfo[] = new $this->objectInfo($result);
			}
			unset($results);
			$numItems = $this->getNumItems('id', $condition, $items_per_page);
			$arrInfor = array();
			$pages['page']      = $page;
			$pages['pages']     = $numItems['pages'];
			$pages['rows']      = $numItems['rows'];
			$pages['start']     = $start;
			$arrInfor['data']   = $objectsInfo;
			$arrInfor['pages']  = $pages;
			return $arrInfor;
		}
		return '';
	}
/*-----------------------------------------------------------------------*
* Function: getObject
* Parameter: WHERE $key
* Return: Array of Info objects
*-----------------------------------------------------------------------*/        
	function getObject($key = '0') {
		$result = $this->getObjects(1,"`id` = '$key'");
		if($result)
			return $result['data'][0];
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function getObjectFromSlug($slug = ''){
		if($slug != ''){
			$object = $this->getObjects(1, "`slug` = '$slug'" . ' AND status = 1');
			if(is_array($object))
				return $object['data'][0];
		}
		return '';
	}
/*-----------------------------------------------------------------------*
* Function: getObjectFromCode
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectFromCode($code) {
		$results = $this->getObjects(1, "code = '$code'", array('code'=>'ASC'), 0, 1);
		if($results) {				
			return $results['data'][0];			
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getObjectsFromCIdByTime
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromCIdByTime($numDate, $cid, $sort = array(), $items_per_page = 0) {
		$startDate 	= dateafter(0);
		$endDate 	= dateafter($numDate);
		$conditions = 'cid = "' . $cid.'" AND  date_created >= "'.$startDate.'"'.' AND  date_created <= "'.$endDate.'"';
		return $this->getObjects(1, $conditions . ' AND status = 1', $sort, $items_per_page);
	} 	
/*-----------------------------------------------------------------------*
* Function: getObjectsFromCId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromCId($page = 1, $cid, $sort = array(), $items_per_page = 0) {
		$arrObjects = $this->getObjects($page, 'cid = "' . $cid . '" AND status = 1', $sort, $items_per_page);
		return $arrObjects;
	}
/*-----------------------------------------------------------------------*
* Function: getObjectFirstOneFromCId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectFirstOneFromCId($cid, $sort = array()) {
		$arrObjects = $this->getObjects(1, 'cid="'.$cid.'" AND status = 1', $sort, 1);
		if(isset($arrObjects['data']))
			return $arrObjects['data'][0];
		return;
	}	
/*-----------------------------------------------------------------------*
* Function: getObjectsFromCIdForHome
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromCIdForHome($page = 1, $cid, $sort = array(), $items_per_page = 0) {
		return $this->getObjects($page, 'cid = "' . $cid . '" AND home = 1 AND status = 1', $sort, $items_per_page);
	}				
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function getObjectFromArrayID($arrId){
		$arrayObject = array();
		if(is_array($arrId)){
			foreach($arrId as $key => $value){
				$arrayObject[] = $this->getObject($value);		
			}	
		}else{
			$arrayObject[] = $this->getObject($arrId);			
		}
		return $arrayObject;	
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getObjectFromArrayField($arrayFile = array()){
		$stringSearch = "";
		$number = count($arrayFile);
		$start = 0;
		foreach($arrayFile as $key => $value){
			if($start == $number-1)
				$stringSearch .= "`$key` = '$value'";
			else
				$stringSearch .= "`$key` = '$value' AND ";
			$start++;
		}
		$stringSearch .= ' AND status=1';
		$object = $this->getObjects(1, $stringSearch);
		if(is_array($object) && is_object($object['data'][0]))
			return $object['data'];
		return false;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getFieldPutArray($field = 'id', $condition = "1=1"){
		$array = array();
		$arrObject = $this->getObjects(1, $condition, array("`$field`" => 'DESC'), "", "");
		if($arrObject){
			foreach($arrObject['data'] as $object){
				$array[$object->getField('id')] = 	$object->getField($field);
			}#foreach
		}#endif
		return array_unique($array);		
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/
	function checkDataExits($arrayFile = array()){
		$stringSearch = "";
		$number = count($arrayFile);
		$start = 0;
		foreach($arrayFile as $key => $value){
			if($start == $number-1)
				$stringSearch .= "`$key` = '$value'";
			else
				$stringSearch .= "`$key` = '$value' AND ";
			$start++;
		}
		$object = $this->getObjects(1, $stringSearch);
		if(is_array($object) && is_object($object['data'][0]))
			return true;
		return false;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function up($oId = 0, $position = 0) {
		if(!$oId) return 0;
		$object = $this->getObjects(1, "`id` <> '$oId' AND `position` <= '$position'", array('`position`' => 'DESC'),0,1);		
		if($object) {
			$nId = $object['data'][0]->getField('id');
			$nPosition = $object['data'][0]->getField('position');
			if($nPosition >= $position) $nPosition = $position - 1;
			$this->update(array('position' => $nPosition), "`id` = '$oId'");
			$this->update(array('position' => $position), "`id` = '$nId'");				
			return 1;
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/			
	function down($oId = 0, $position = 0) {
		if(!$oId) return 0;
		$object = $this->getObjects(1, "`id` <> '$oId' AND `position` <= '$position'", array('`position`' => 'DESC'),0,1);		
		if($object) {
			$nId = $object['data'][0]->getField('id');
			$nPosition = $object['data'][0]->getField('position');
			$this->update(array('position' => $nPosition), "`id` = '$oId'");
			$this->update(array('position' => $position), "`id` = '$nId'");				
			return 1;
		}
		return 0;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function creatOption($values, $id = 'id', $name = 'vn_name', $flag="/") {
		$arrayObject = $this->getObjects(1, 'status = 1', array($name=>"DESC"),"","");
		$string = "";	
		//$string .=  "<option value=''>".$flag."</option>";
		if(is_array($values)){
			foreach($arrayObject['data'] as $object){
				$selected = '';
				foreach($values as $key){
					if($object->getField($id)==$key)
						$selected = 'selected';			
				}
				$string .= "<option ".$selected." value='".$object->getField($id)."'>".$object->getField($name)."</option>";	
			}			
		}else{
			foreach($arrayObject['data'] as $object){
				$selected = $object->getField($id)==$values?'selected':'';			
				$string .= "<option ".$selected." value='".$object->getField($id)."'>".$object->getField($name)."</option>";	
			}			
		}
		return $string;

	}
/*-----------------------------------------------------------------------*
* Function: getOptionPI
* Parameter: WHERE condition
* $cid = parentid
* $value = value default or valule was chosice
* $sort = array for order
* $nameid = name of field primary
* $nameTitle = name of field display
* $level: this function get category with this level
* $pace: ...
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getOptionAllCategory($cid = 0, $value = '', $sort = array('position' => 'ASC'), $nameid = "id", $nameTitle = "name",  $level = 3, $pace = "|"){
		$str = '&nbsp;';
		$objectCategory = $this->getObjects(1, "`cid` = $cid AND status = 1", $sort, '');
		if($objectCategory) $objectCategory = $objectCategory['data'];
		if(count($objectCategory)>0 && $level > 0):
			$pace .= '---';
			--$level;
			foreach($objectCategory as $object):
				$str .= "<option value='".$object->getField($nameid)."'".($object->getField($nameid)==$value?" selected=\"selected\"":"").">" . $pace . $object->getField($nameTitle). "</option>";
				$str .= $this->getOptionAllCategory($object->getField($nameid), $value, $sort, $nameid, $nameTitle, $level, $pace);
			endforeach;
		endif;
		unset($objectCategory);
		return $str;
	}	
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/			
	function changePosition($oId = 0, $position = 0) {
		if(!$oId) return 0;
		if($this->update(array('position' => $position), "`id` = '$oId'")) return 1;
		return 0;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function addData($object) {
			$result = $this->add($object,'id',0);
			if($result)
				return $result;
			return 0;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function updateData(&$data,$id) {
		// Neu truyen vao id <=0 thi coi nhu day la truong hop them moi du lieu
		if($id<=0) return $this->addData($data);
		unset($data['id']);
		if($this->update($data, "`id` = '$id'")) return 1;
		return 0;
	}
/*-----------------------------------------------------------------------*
* Function: upload
* Parameter: WHERE condition
* Return: array
* Note: it's only use in site admin
*-----------------------------------------------------------------------*/		
	function upload($avatar, $opinfo){
		global $messages;
		$isUpload = $this->isUploadFile($avatar['name']);
		$arrayReturn = array(
								'status'	=>	false, 
								'name'		=>	'',
								'messages'	=> 	'',
							);
		if ($isUpload==1){
			include_once(ROOT_PATH."classes/core/upload.class.php");
			/* Duong dan */
			$dirnews 		 = IMAGES."/".$opinfo->getField('code');
			$gallery_avatar  = $dirnews."/".AVATAR;
			$gallery_medium  = $dirnews."/".MEDIUM;
			$gallery_big 	 = $dirnews."/".BIG;
			$gallery_storage = $dirnews."/".STORAGE;
			
			$avatarUpload = new Upload($avatar);
			$copy   = $avatarUpload->moveFile($gallery_storage);
			if(!$copy){
				$arrayReturn['status'] = false;
				$arrayReturn['messages'] = $messages['cannotuploadfile'] . $messages['check_type']; 
			}else{
				$arrayReturn['status'] = true;
				$arrayReturn['messages'] = '';
				$arrayReturn['name'] = $avatarUpload->getNameFile();
				$ext = explode(".", $avatarUpload->getNameFile());
				$ext = strtolower($ext[count($ext)-1]);
				if(preg_match("/".ALLOW_IMAGE_TYPES."/",$ext)){
					$avatarUpload->resize($gallery_storage, $gallery_avatar, $opinfo->getField('wavatar'), $opinfo->getField('havatar'));
					$avatarUpload->resize($gallery_storage, $gallery_medium, $opinfo->getField('wmedium'), $opinfo->getField('hmedium'));
					$avatarUpload->resize($gallery_storage, $gallery_big, 	 $opinfo->getField('wbig'),    $opinfo->getField('hbig'));
				}
			}
		}
		return $arrayReturn;	
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function getRecord($id) {
		$result = $this->select('id',"`id` = '$id'");
		if($result) return 1;
		return "";
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function isUploadFile($str) {
			$ext = explode(".", $str);
			$ext = strtolower($ext[count($ext)-1]);
			if(preg_match("/".ALLOW_FILE_TYPES."/",$ext)) return 1;
			return 0;
	}
/*-----------------------------------------------------------------------*
* Function: getSubCategory
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getSubCategory($id) {
		$objSub = $this->getObjects(1, "status =1 AND `cid` = '$id'", array('position'=>'ASC'), "");
		if(isset($objSub['data'])) return $objSub['data'];
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getSubCategoryHome
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getSubCategoryHome($id) {
		$objSub = $this->getObjects(1, "status =1 AND `cid` = '$id' AND home=1", array('position'=>'ASC'), "");
		if($objSub) return $objSub['data'];
		return;
	}	

/*-----------------------------------------------------------------------*
* Function: getObjectsFromArrayCId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromArrayCId($page = 1, $array_id = array(), $sort = array(), $items_per_page = 0) {
		if(count($array_id)>0){
			$string_id = implode(',',$array_id);
			return $this->getObjects($page, '`cid` IN('.$string_id.')' . ' AND status = 1', $sort, $items_per_page);
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getObjectsFromArrayCId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/	
	function getObjectsFront($page = 1, $condition = '1>0', $sort = array(), $items_per_page = 0) {
		return $this->getObjects($page, $condition, array('`date_created`' => 'DESC'), $items_per_page);
	}
/*-----------------------------------------------------------------------*
* Function: getOtherCategory
* Parameter: WHERE $id
* Return: Array of Info objects
*-----------------------------------------------------------------------*/	
	function getOtherCategory($id) {
		// Get Object
		$objCurrent = $this->getObject($id);
		$arrObjects = $this->getObjects(1, "cid=".$objCurrent->getField('cid')." AND id <>".$objCurrent->getField('id'));
		if(isset($arrObjects['data'])){
			return $arrObjects['data'];	
		}
		return;
	}		
/*-----------------------------------------------------------------------*
* Function: getObjectsFromArrayId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getNumRecordCId($cid) {
		$result = $this->getNumItems('id',"`cid` = '$cid' AND status = 1", $items_per_page = 0);
		if($result) return $result;
		return "";
	}		
/*-----------------------------------------------------------------------*
* Function: getObjectsFromStringCId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromStringCId($page = 1, $arrCId, $sort = array(), $items_per_page = 0) {
		$numCId = count($arrCId);
		if($numCId<=0) return;
		$strSQL = "status = 1 AND (";
		foreach($arrCId as $key => $cid){
			if($key+1 == $numCId)
				$strSQL .= 'stringcid LIKE \'%"' . $cid . '"%\')';
			else
				$strSQL .= 'stringcid LIKE \'%"' . $cid . '"%\' OR ';	
		}
		return $this->getObjects($page, $strSQL, $sort, $items_per_page);
	}	
/*-----------------------------------------------------------------------*
* Function: getObjectsFromArrayId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromArrayId($page = 1, $array_id = array(), $sort = array(), $items_per_page = 0) {
		if(count($array_id)>0){		
			if(!$page) $page = 1;
			$start = ($page -1) * $items_per_page;
			$string_id = implode(',',$array_id);
			$result = $this->getObjects(1, 'id IN('.$string_id.')' . ' AND status = 1', $sort, $items_per_page);
			if($result) return $result['data'];
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getIdSubCategory($cid, $flag = 1) {
		$objectCategorys = $this->getObjects(1, "status =1 AND `cid` = '$cid'", array('position'=>'ASC'), 0);
		if($objectCategorys) $objectCategorys = $objectCategorys['data'];
		if(count($objectCategorys)>0):
			$arrayId = array();
			if($flag == 1) $arrayId[] = $cid;
			foreach($objectCategorys as $object):
				$arrayId[] = $object->getField('id');
				$havearray = $this->getIdSubCategory($object->getField('id'), 0);
				if(count($havearray)>0)
					foreach($havearray as $id):
						$arrayId[] = $id;
					endforeach;
				unset($havearray);
			endforeach;
			return $arrayId;
		endif;
		unset($objectCategorys);
		return array($cid);
	}	
/*-----------------------------------------------------------------------*
* Function: getIdSubCategoryALL()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getIdSubCategoryALL($cid, $flag = 1) {
		$objectCategorys = $this->getObjects(1, "`cid` = '$cid'",array('position'=>'ASC'), 0);
		if($objectCategorys) $objectCategorys = $objectCategorys['data'];
		if(count($objectCategorys)>0):
			$arrayId = array();
			if($flag == 1) $arrayId[] = $cid;
			foreach($objectCategorys as $object):
				$arrayId[] = $object->getField('id');
				$havearray = $this->getIdSubCategory($object->getField('id'), 0);
				if(count($havearray)>0)
					foreach($havearray as $id):
						$arrayId[] = $id;
					endforeach;
				unset($havearray);
			endforeach;
			return $arrayId;
		endif;
		unset($objectCategorys);
		return array($cid);
	}
/*-----------------------------------------------------------------------*
* Function: getParent()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getParent($id) {
		$objects = $this->getObjects(1,"cid=".$cid);
		if(isset($objects['data'])) {
			return $objects['data'][0];
		}
		return;
	}		
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getParentName($cid) {
		$object = $this->getObject($cid);
		if($object) {
			return $object->getFieldLang('name');
		}
		return "/";
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function comboListObjects($pk = 'id', $title = 'vn_name', $value='', $condition = '1=1', $sort = array('position' => 'ASC')){		
		$results = $this->select("`$pk`,`$title`", $condition, $sort);
		$str = '';
		if($results){
			foreach($results as $key => $result) {
				$str .= "<option value=\"".$result[$pk]."\"".($result[$pk]==$value?" selected":"").">".$result[$title]."</option>";
			}
		}				
		return $str;
	}
/*-----------------------------------------------------------------------*
* Function: getPathParrent()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getPathParrent($id) {
		$arrPath = array();
		$cid = 0;
		$objectCategorys = $this->getObject($id);
		if(is_object($objectCategorys))
			$cid = $objectCategorys->getField("cid");
		if($cid!=0){
			$arrPath = $this->getPathParrent($cid);	
		}
		$arrPath[] = $objectCategorys;
		unset($cid);
		unset($objectCategorys);
		return $arrPath;
	}	
/*-----------------------------------------------------------------------*
* Function: getPathParrent()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getParrentRoot($id) {
		$arrPath = array();
		$cid = 0;
		$objectCategorys = $this->getObject($id);
		if(is_object($objectCategorys))
			$cid = $objectCategorys->getField("cid");
		if($cid==0){
			return $objectCategorys;	
		}else{
			return $this->getParrentRoot($cid);	
		}
	}	
/*-----------------------------------------------------------------------*
* Function: getRelatedProduct lay san pham cung loai
* Parameter: WHERE catproduct
* Return: Array Bid of manufacturers
*-----------------------------------------------------------------------*/		
	function getRelatedProduct($page = 1, $id, $sort = array(), $items_per_page = 0){
		$news = $this->getObject($id);
		if($news){
			$condition = 'cid = ' . $news->getField('cid') . ' AND id NOT IN('. $id .')';
			return $this->getObjects($page, $condition, $sort, $items_per_page);
		}
		return;
	}	
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/		
	function optionAllCategories($cid = 0, $value = '', $lang = 'vn', $sort = array('position' => 'ASC'), $level = 0, $leveloption = 1){
		$str = '&nbsp;';	
		$objectCategory = $this->getSubCategory($cid);
		if(count($objectCategory)>0&&$leveloption>0):
			$pace = '';
			for($i=$level; $i>0; --$i):
				$pace .= '&nbsp;&nbsp;&nbsp;'; 
			endfor;	
			++$level;
			--$leveloption;			
			foreach($objectCategory as $object):		
				$str .= "<option value='".$object->getField('id')."'".($object->getField('id')==$value?" selected=\"selected\"":"").">" . $pace . $object->getFieldLang('name'). "</option>";				
				$str .= $this->optionAllCategories($object->getField('id'), $value, $lang, $sort, $level, $leveloption);
			endforeach;
		endif;
		unset($objectCategory);
		return $str;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getParentSlug($cid) {
		$result = $this->select('slug', "id=$cid");
		if($result) 
			return 1;
		else return 0;
	}
/*-----------------------------------------------------------------------*
* Function: getFieldPutArray()
* Parameter: $field, $condition
* Return: array field
*-----------------------------------------------------------------------*/	
	function getCategoryNameFromId($lang = 'vn', $cid) {
		$result = $this->select($lang."_name", "id=$cid");
		if($result) 
			return 1;
		else return 0;
	}
/*-----------------------------------------------------------------------*
* Function: getIdFromIdLevelTop lay tat ca cac Id tu Id cao hon
* Parameter: WHERE catproduct
* Return:array ID
*-----------------------------------------------------------------------*/			
	function getIdFromIdLevelTop($cid) {
		$results = $this->select("id", "status =1 AND `cid` = '$cid'",array('position'=>'ASC'));
		if($results) {
			$categoryInfos = array();
			foreach($results as $key => $result) {
				$categoryInfos[]= $result['id'];
			}
			return implode(",",$categoryInfos).",$cid";
		}
		return ($cid);
	}		
/*-----------------------------------------------------------------------*
* Function: cleanTrash
* Parameter: WHERE condition
* Return: true or false
*-----------------------------------------------------------------------*/			
	function cleanTrash($condition = '1=1'){
		$dem = 0;
		$objectDelete = $this->getObjects(1, $condition, '', '');
		if($objectDelete) $objectDelete = $objectDelete['data'];
		foreach($objectDelete as $object){
			$this->delete("id = " . $object->getField('id'));
		}
		return true;
	}	
}
?>